//
//  PKHomeSearchViewController.swift
//  Panku
//
//  Created by mac on 2017/2/13.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import SnapKit
import YYKit
import ReactiveCocoa
import ReactiveSwift
import Result

class PKHomeSearchViewController: PKBaseViewController {
    
    //MARK: - init
    init(viewModel:PKHomeSearchViewModel){
        super.init(nibName: nil, bundle: nil)
        self.viewModel = viewModel
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - properties
    //viewModel
    var viewModel:PKHomeSearchViewModel?
    
    //UI
    lazy var searchBar = UISearchBar()
    
    //MARK: - UI
    override func setupRootView() {
        view.backgroundColor = UIColor.white
        
    }
    
    override func setupSubViews() {
        view.addSubview(searchBar)
        searchBar.searchBarStyle = .default
        searchBar.placeholder = "请输入昵称/放克号"
    }
    
    override func setupSubViewsConstraints() {
        searchBar.frame = CGRect(x: 0, y: 60, width: APP_WIDTH, height: 44)
    
    }
    
    override func setupNavigationBar() {
        
        //title
        self.title = "搜索"
        
        //rightBarButtonItem
        let dismissButton = PKBaseButton.createButton()
        dismissButton.setTitleColor(UIColor.black, for: .normal)
        dismissButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        dismissButton.setTitle("取消", for: .normal)
        dismissButton.addBlock(for: .touchUpInside) { (_) in
            self.dismiss(animated: true, completion: { 
                //finish callback
            })
        }
        
        let dismissBarbuttonItem = UIBarButtonItem(customView: dismissButton)
        self.navigationItem.rightBarButtonItem = dismissBarbuttonItem
        
    }
    
    override func bindViewModel() {
        
        let button = UIButton(type: .custom)
        
    }
    
    private func signalForSearchBar() -> Signal<String, NoError>{
        return searchBar.reactive.continuousTextValues.skipNil().skipRepeats().throttle(1, on: QueueScheduler.main)
    }
    
//    private func signalForSearchResult() -> Signal<String,NSError>{
//        return self.signalForSearchBar().flatMap(.latest) { (text) -> SignalProducer<String, NSError> in
//            return self.viewModel!.signalProducerForSearchResult()
//        }.map({ (text) -> String in
//            return text
//        })
//        
//    }

}
